package com.work.blog.controller;

import com.work.blog.constant.Constant;
import com.work.blog.model.Result;
import com.work.blog.model.UserInfo;
import com.work.blog.service.UserService;
import com.work.blog.utils.JwtUtils;
import com.work.blog.utils.SecurityUtils;
import jakarta.servlet.http.HttpServletRequest;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.Map;

/**
 * Created with IntelliJ IDEA.
 * Description:
 * User:DELL
 * Date:2025-06-17
 * Time:15:56
 */
@Slf4j
@RequestMapping("/user")
@RestController
public class UserController {
    @Autowired
    private UserService userService;
    @RequestMapping("/login")
    public Result login(String username, String password){
        log.info("UserController#login接收参数：username{},password{}",username,password);
        //1.参数校验合法性
        //校验密码是否正确
        //密码正确，返回token
        //密码错误，返回错误信息
        if(!StringUtils.hasLength(username)||!StringUtils.hasLength(password)){
            return Result.fail("账号或密码不能为空");
        }
        //从数据库中查找用户
        UserInfo userInfo=userService.selectByName(username);
        //用户不存在
        if(userInfo==null){
            log.error("用户不存在");
            return Result.fail("用户不存在");
        }
        //密码错误,验证用户输入的和数据库中获取的是否相等（进行了加密）
        if(!SecurityUtils.verify(password,userInfo.getPassword())){
            log.error("密码错误");
            return Result.fail("密码错误");
        }
        //密码正确，返回token
        Map<String,Object> claim=new HashMap<>();
        claim.put(Constant.TOKEN_ID,userInfo.getId());
        claim.put(Constant.TOKEN_USERNAME,userInfo.getUserName());
        String token=JwtUtils.genJwtToken(claim);
        log.info("UserController#login 返回结果token：{}",token);
        return Result.success(token);
    }

    //获取当前登录的用户信息
    @RequestMapping("/getUserInfo")
    public UserInfo getLoginUserInfo(HttpServletRequest request){
        log.info("获取用户信息...");
        //获取token
        String token = request.getHeader(Constant.REQUEST_HEADER_TOKEN);//拿到token
        //从token中获取登录用户的ID
        Integer userId = JwtUtils.getIdByToken(token);
        if (userId==null){
            //用户未登录
            return null;
        }
        UserInfo userInfo = userService.selectById(userId);
        return userInfo;
    }

    //获取作者信息
    @RequestMapping("/getAuthorInfo")
    public UserInfo getAuthorInfo(Integer blogId){
    //1.根据博客id，获取作者ID
        //2.根据作者id,获取作者信息
        if (blogId<=0){
            return null;
        }
        UserInfo userInfo=userService.getAuthorInfo(blogId);
        return userInfo;
    }
}
